/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.websai.servlets;

import com.sai.clases.Alumno;
import com.sai.clases.Calificacion;
import com.sai.transacciones.TAlumno;
import com.sai.transacciones.TCalificacion;
import com.sai.utils.Utilities;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Eduardo
 */
public class Integradora extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
          int metodo = Integer.parseInt(request.getParameter("metodo"));
            switch(metodo){
                case 0:
                    Registrar(request, response);
                    break;
                case 1:
                    
                    break;
                case 2:
                   RegistrarCalificacion(request,response);
                    break;
                case 3:
                   EliminarAlumno(request,response);
                    break;
                 case 4:
                   BuscarAlumno(request,response);
                    break;
            }
        } finally {            
            out.close();
        }
    }
       public void Registrar(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try {
            String nombre = request.getParameter("Nombre");
            String ap = request.getParameter("APaterno");
            String am = request.getParameter("AMaterno");
            String matricula = request.getParameter("Matricula");
            String grupo = request.getParameter("idGrupo");
            String proyecto = request.getParameter("Proyecto");
            
            
            if(Utilities.isReallyEmptyOrNull(nombre) && Utilities.isReallyEmptyOrNull(ap) && Utilities.isReallyEmptyOrNull(am)
                    && Utilities.isReallyEmptyOrNull(matricula) && Utilities.isReallyEmptyOrNull(grupo)
                    && Utilities.isReallyEmptyOrNull(proyecto)){
                out.print("Blanco");
            } else if(Utilities.isReallyEmptyOrNull(nombre)) {
                out.print("Nombre");
            } else if(Utilities.isReallyEmptyOrNull(ap)) {
                out.print("AP");
            } else if(Utilities.isReallyEmptyOrNull(am)) {
                out.print("AM");
            } else if(Utilities.isReallyEmptyOrNull(matricula)) {
                out.print("Matricula");
            } else if(Utilities.isReallyEmptyOrNull(grupo)) {
                out.print("idGrupo");
            } else if(Utilities.isReallyEmptyOrNull(proyecto)) {
                out.print("Proyecto");
            } else {
                Alumno alumno = new Alumno();
                alumno.setNombre(nombre);
                alumno.setApellidoP(ap);
                alumno.setApellidoM(am);
                alumno.setMatricula(matricula);
                alumno.setNombreProyecto(proyecto);
                alumno.setIdGrupo(grupo);
                
                String respuesta = TAlumno.Registrar(alumno);
                if(respuesta.equals("OK")){
                    out.print("OK");
                } else {
                    out.print(respuesta);
                }
            } 
        } finally {
            out.close();
        }
    }
        public void RegistrarCalificacion(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try {
            String matricula = request.getParameter("Matricula");
            String rubro = request.getParameter("Rubro");
            String calificacion = request.getParameter("Calificacion");
            String idP = request.getParameter("idProfesor");
            String idA = request.getParameter("idAlumno");
            
            
            if(Utilities.isReallyEmptyOrNull(matricula) && Utilities.isReallyEmptyOrNull(rubro) && Utilities.isReallyEmptyOrNull(calificacion)
                    && Utilities.isReallyEmptyOrNull(idP) && Utilities.isReallyEmptyOrNull(idA)
                    ){
                out.print("Blanco");
            } else if(Utilities.isReallyEmptyOrNull(matricula)) {
                out.print("MAtricula");
            } else if(Utilities.isReallyEmptyOrNull(rubro)) {
                out.print("AP");
            } else if(Utilities.isReallyEmptyOrNull(calificacion)) {
                out.print("AM");
            } else if(Utilities.isReallyEmptyOrNull(idP)) {
                out.print("idProfesor");
            } else if(Utilities.isReallyEmptyOrNull(idA)) {
                out.print("idAlumno");
            } else {
                
               Calificacion calificaciones= new Calificacion();
                calificaciones.setMatricula(matricula);
                calificaciones.setRubro(rubro);
                calificaciones.setCalificacion(calificacion);
                calificaciones.setIdmateria(Integer.SIZE);
                
                
                String respuesta = TCalificacion.Registrar(calificaciones);
                if(respuesta.equals("OK")){
                    out.print("OK");
                } else {
                    out.print(respuesta);
                }
            } 
        } finally {
            out.close();
        }
    }

         public void EliminarAlumno(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter(); 
        try {
            String id = request.getParameter("id");
            TAlumno obj = new TAlumno();
            if(obj.Eliminar(id)){
            out.print("eliminado");
            }else{
            out.print("no encontrado");
            }
        } finally {            
            out.close();
        }
     }
         
          public void BuscarAlumno(HttpServletRequest request, HttpServletResponse response) throws IOException {
          response.setContentType("text/xml;charset=UTF-8");
                
          PrintWriter out = response.getWriter();
        try {
            String id = request.getParameter("id");
            TAlumno obj = new TAlumno();
            if(obj.Buscar(id)){
                out.print(obj);
            } else {
                out.print("<?xml version='1.0' ?><alumno><estado>ERROR</estado></alumno>");
            }
        } finally {            
            out.close();
        }
        
          }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
